package com.onlyxiahui.extend.query.hibernate;

import java.io.IOException;
import java.io.StringWriter;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.onlyxiahui.extend.query.hibernate.handler.xml.QueryDecode;
import com.onlyxiahui.extend.query.hibernate.handler.xml.QueryItem;
import com.onlyxiahui.extend.query.hibernate.handler.xml.QueryItemException;
import com.onlyxiahui.extend.query.hibernate.handler.JpaHandler;

import freemarker.cache.StringTemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;
import freemarker.template.Version;

/**
 * 
 * Date 2018-12-28 16:47:08<br>
 * Description
 * 
 * @author XiaHui<br>
 * @since 1.0.0
 */
public class QueryContext {

	private final QueryDecode qd = new QueryDecode();
	private final Map<String, QueryItem> queryItemMap = new ConcurrentHashMap<String, QueryItem>();

	private final StringTemplateLoader templateLoader = new StringTemplateLoader();
	private final Version version = Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS;
	private final Configuration config = new Configuration(version);

	private String[] locations;
	private JpaHandler jpaHandler = new JpaHandler();

	public QueryContext() {
		config.setTemplateLoader(templateLoader);
		config.setObjectWrapper(new DefaultObjectWrapper(version));
	}

	public String[] getConfigLocations() {
		return locations;
	}

	public void setConfigLocations(String[] configLocations) {
		this.locations = configLocations;
	}

	public void load() throws QueryItemException {
		queryItemMap.clear();
		if (null != locations) {
			for (String configLocation : locations) {
				if (null != configLocation && !"".equals(configLocation)) {
					List<QueryItem> queryItemList = qd.getQueryItemList(configLocation);
					for (QueryItem item : queryItemList) {
						String key = getKey(item);
						if (queryItemMap.containsKey(key)) {
							throw new QueryItemException("命名空间为：" + item.getSpaceName() + "，查询语句名为：" + item.getName() + "，有重复！");
						} else {
							queryItemMap.put(key, item);
							templateLoader.putTemplate(key, item.getContent());
						}
					}
				}
			}
		}
	}

	private String getKey(QueryItem item) {
		StringBuilder key = new StringBuilder();
		key.append(item.getSpaceName());
		key.append(".");
		key.append(item.getName());
		return key.toString();
	}

	public QueryItem getQueryItem(String name) {
		return queryItemMap.get(name);
	}

	public String getQueryContent(String name, Map<String, Object> valueMap) {
		StringWriter writer = new StringWriter();
		Template template;
		try {
			template = config.getTemplate(name, "UTF-8");
			template.process(valueMap, writer);
		} catch (Exception e) {
			e.printStackTrace();
		}
		try {
			writer.flush();
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		String sql = writer.toString();
		return sql;
	}

	public String getQueryContent(String name, QueryWrapper queryWrapper) {
		Map<String, Object> valueMap = (null == queryWrapper) ? null : queryWrapper.getParameterMap();
		return getQueryContent(name, valueMap);
	}

	public JpaHandler getJpaHandler() {
		return jpaHandler;
	}
}
